using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

namespace JH
{
    /// <summary>
    ///     toggle组件打开时设置颜色，关闭时设置颜色
    /// </summary>
    [ExecuteAlways]
    public class ToggleSetColor : BaseToggleCustomEvent
    {
        /// <summary>
        ///     toggle打开时的颜色
        /// </summary>
        [SerializeField,Header("开启时的颜色")] private Color onToggleOnColor;

        /// <summary>
        ///     toggle关闭时的颜色
        /// </summary>
        [SerializeField,Header("关闭时的颜色")] private Color onToggleOffColor;

        /// <summary>
        ///     需要被设置颜色的图形
        /// </summary>
        [SerializeField] public List<Graphic> graphicsList = new();
        
        protected override void OnToggleOff()
        {
            base.OnToggleOff();
            SetColor(false);
        }

        protected override void OnToggleOn()
        {
            base.OnToggleOn();
            SetColor(true);
        }

        private void SetColor(bool isOn)
        {
            foreach (var each in graphicsList.Where(i => i != null))
                each.color = isOn ? onToggleOnColor : onToggleOffColor;
        }
    }
}